package me.zogodo;

import java.sql.*;

/**
 * Created by zogod on 2016/12/18.
 */
public class OracleHelper
{
    private static String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
    private static String jar = "oracle.jdbc.driver.OracleDriver";
    private static String username = "system";
    private static String password = "123456";

    class Resources
    {
        Connection conn;
        Statement stmt;
        PreparedStatement pstmt;
        ResultSet rset;

        void close() throws SQLException
        {
            if (conn != null) conn.close();
            if (stmt != null) stmt.close();
            if (pstmt != null) pstmt.close();
            if (rset != null) rset.close();
        }
    }

    public Resources Select(String sql)
    {
        Resources rsc = new Resources();
        try
        {
            Class.forName(jar);
            rsc.conn = DriverManager.getConnection(url, username, password);
            rsc.stmt = rsc.conn.createStatement();
            rsc.rset = rsc.stmt.executeQuery(sql);
            return rsc;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            try{ rsc.close(); }
            catch (SQLException e) { }
        }
    }

    public static ResultSet SelectPra(String sql)
    {
        sql = "select * from admin where id=? and usr=?";
        Connection conn = null;
        PreparedStatement pstmt = null;
        try
        {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 31);
            pstmt.setString(2, "zogodo");
            //int x = pstmt.executeUpdate();
            ResultSet rs = pstmt.executeQuery(sql);
            return rs;
        }
        catch (SQLException e)
        {
            e.printStackTrace();
            return null;
        }
    }

}
